import _regeneratorRuntime from "@babel/runtime/regenerator";
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";

/**
 * model 租户级期间定义
 * @date: 2018-7-12


 */
import { searchHeader, getDetail as _getDetail, create as _create, edit as _edit } from "../services/costCenterService";
import { getResponse, createPagination } from 'utils/utils';

function dealDataState(data) {
  // 处理行 处理字段为update
  var config = [];

  if (Array.isArray(data) && data.length > 0) {
    config = data.map(function (item) {
      return _objectSpread(_objectSpread({}, item), {}, {
        _status: 'update'
      });
    });
  }

  return config;
}

export default {
  namespace: 'costCenter',
  state: {
    periodHeader: {
      // TabPane: "期间定义"
      list: [],
      // 数据列表
      pagination: {} // 分页参数

    },
    detail: {}
  },
  effects: {
    // 获取"期间定义(TabPane)"数据
    searchPeriodHeader: /*#__PURE__*/_regeneratorRuntime.mark(function searchPeriodHeader(_ref, _ref2) {
      var payload, call, put, result;
      return _regeneratorRuntime.wrap(function searchPeriodHeader$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              payload = _ref.payload;
              call = _ref2.call, put = _ref2.put;
              _context.t0 = getResponse;
              _context.next = 5;
              return call(searchHeader, payload);

            case 5:
              _context.t1 = _context.sent;
              result = (0, _context.t0)(_context.t1);

              if (!result) {
                _context.next = 10;
                break;
              }

              _context.next = 10;
              return put({
                type: 'updateState',
                payload: {
                  periodHeader: {
                    list: dealDataState(result.content),
                    pagination: createPagination(result)
                  }
                }
              });

            case 10:
              return _context.abrupt("return", result);

            case 11:
            case "end":
              return _context.stop();
          }
        }
      }, searchPeriodHeader);
    }),
    create: /*#__PURE__*/_regeneratorRuntime.mark(function create(_ref3, _ref4) {
      var payload, call, result;
      return _regeneratorRuntime.wrap(function create$(_context2) {
        while (1) {
          switch (_context2.prev = _context2.next) {
            case 0:
              payload = _ref3.payload;
              call = _ref4.call;
              _context2.next = 4;
              return call(_create, payload);

            case 4:
              result = _context2.sent;
              return _context2.abrupt("return", getResponse(result));

            case 6:
            case "end":
              return _context2.stop();
          }
        }
      }, create);
    }),
    edit: /*#__PURE__*/_regeneratorRuntime.mark(function edit(_ref5, _ref6) {
      var payload, call, result;
      return _regeneratorRuntime.wrap(function edit$(_context3) {
        while (1) {
          switch (_context3.prev = _context3.next) {
            case 0:
              payload = _ref5.payload;
              call = _ref6.call;
              _context3.next = 4;
              return call(_edit, payload);

            case 4:
              result = _context3.sent;
              return _context3.abrupt("return", getResponse(result));

            case 6:
            case "end":
              return _context3.stop();
          }
        }
      }, edit);
    }),
    getDetail: /*#__PURE__*/_regeneratorRuntime.mark(function getDetail(_ref7, _ref8) {
      var payload, put, call, result;
      return _regeneratorRuntime.wrap(function getDetail$(_context4) {
        while (1) {
          switch (_context4.prev = _context4.next) {
            case 0:
              payload = _ref7.payload;
              put = _ref8.put, call = _ref8.call;
              _context4.next = 4;
              return call(_getDetail, payload);

            case 4:
              result = _context4.sent;

              if (!result) {
                _context4.next = 8;
                break;
              }

              _context4.next = 8;
              return put({
                type: 'updateState',
                payload: {
                  detail: result
                }
              });

            case 8:
              return _context4.abrupt("return", getResponse(result));

            case 9:
            case "end":
              return _context4.stop();
          }
        }
      }, getDetail);
    })
  },
  reducers: {
    updateState: function updateState(state, _ref9) {
      var payload = _ref9.payload;
      return _objectSpread(_objectSpread({}, state), payload);
    }
  }
};